package com.harmonyos.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.harmonyos.domain.Limit;
import com.harmonyos.domain.TbXinli;
import com.harmonyos.domain.msg;
import com.harmonyos.mapper.TbXinliMapper;
import com.harmonyos.service.TbXinliService;
import com.harmonyos.swagger.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * TODO
 *
 * @Description
 * @Author lanmo
 * @Date 2023/1/16 11:06
 **/
@Service
@Slf4j
public class TbXinliServiceImpl implements TbXinliService {
    @Resource
    private TbXinliMapper tbXinliMapper;
    @Resource
    private RestTemplate restTemplate;
    @Autowired
    private RedisUtil redisUtil;
    private final String key = "xinli:";

    @Override
    public void addXinli() {
        for (int i = 1; i <= 40; i++) {
            String s = restTemplate.getForObject("https://apis.juhe.cn/fapig/soup/query?key=4179859c049a00c31e147939ab621883", String.class, (Object) null);
            log.info(s);
            msg msg = JSONUtil.toBean(s, msg.class);
            tbXinliMapper.insert(msg.getResult());
        }
    }

    @Override
    public String selectAll() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            List<TbXinli> tbXinlis=new ArrayList<TbXinli>();
            Set<String> keys = redisUtil.keys(key);
            if (keys.size()!=0){
                keys.forEach(key ->{
                   tbXinlis.add((TbXinli) redisUtil.get(key));
                });
            }else {
                tbXinlis.addAll(tbXinliMapper.selectList(null));
                tbXinlis.forEach(tbXinli -> {
                    redisUtil.set(key+tbXinli.getId(), (Object) tbXinli,60*60);
                });
            }
            map.put("code", "200");
            map.put("data", tbXinlis);
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSONUtil.toJsonStr(map);
    }

    @Override
    public String LimitXinli(String json) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            Limit<TbXinli> limit = JSONUtil.toBean(json, Limit.class);
            PageHelper.startPage(limit.getCurrent(), limit.getSize());
            List<TbXinli> tbXinlis = tbXinliMapper.LimitXinli(limit.getKey());
            PageInfo<TbXinli> info = new PageInfo<TbXinli>(tbXinlis);
            List<TbXinli> list = info.getList();
            map.put("code", "200");
            map.put("data", list);
            map.put("total", info.getTotal());
        } catch (Exception e) {
            map.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(map);
    }

    @Override
    public String deleteAll() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            Set<String> keys = redisUtil.keys(key);
            if (keys.size()!=0) {
                redisUtil.removeAll(key);
            }
            tbXinliMapper.delete(null);
            map.put("code", "200");
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSONUtil.toJsonStr(map);
    }

    @Override
    public String deleteById(String id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            if (redisUtil.hasKey(key+id)) {
                redisUtil.del(key+id);
            }
            tbXinliMapper.deleteById(id);
            map.put("code", "200");
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSONUtil.toJsonStr(map);
    }

    @Override
    public String addXinli(TbXinli tbXinli) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            tbXinliMapper.insert(tbXinli);
            redisUtil.set(key+tbXinli.getId(),(Object)tbXinli,60*60);
            map.put("code", "200");
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSONUtil.toJsonStr(map);
    }

    @Override
    public String updateXinli(TbXinli tbXinli) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            if (redisUtil.hasKey(key+tbXinli.getId())) {
                redisUtil.set(key+tbXinli.getId(), (Object)tbXinli,60*60);
            }
            tbXinliMapper.updateById(tbXinli);
            map.put("code", "200");
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSONUtil.toJsonStr(map);
    }
}
